import 'package:flutter/material.dart';

/// 2022年更新  适配 flutter 3.0
class Autocompltet01 extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return Autocompltet01State();
  }
}

class Autocompltet01State extends State with SingleTickerProviderStateMixin {
  ///初化函数
  /// 在Widget创建的时候
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Padding(
          child: buildCenter(),
          padding: EdgeInsets.all(20),
        ),
      ),
    );
  }

  buildCenter() {
    List<TestBean> list = [
      TestBean("张三"),
      TestBean("张三584"),
      TestBean("李四"),
      TestBean("李四3456"),
    ];
    return Autocomplete<TestBean>(
      ///数据自动过滤
      optionsBuilder: (TextEditingValue textEditingValue) {
        return list
            .where((TestBean continent) => continent.name
                .toLowerCase()
                .startsWith(textEditingValue.text.toLowerCase(),))
            .toList();
      },
      ///回显示的内容
      displayStringForOption: (TestBean option) => option.name,
    );
  }
}

class TestBean {
  String name = "";
  TestBean(this.name);
}
